package com.firebird.app.kotlin.stock.ui.addbank.presenter

import com.firebird.app.kotlin.stock.base.BasePresenter
import com.firebird.app.kotlin.stock.data.BankData
import com.firebird.app.kotlin.stock.data.SimpleBean
import com.firebird.app.kotlin.stock.net.RxScheduler.Flo_io_main
import com.firebird.app.kotlin.stock.ui.addbank.contract.AddBankContract
import com.firebird.app.kotlin.stock.ui.addbank.model.AddBankModel
import okhttp3.RequestBody
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response


class AddBankPresenter : BasePresenter<AddBankContract.View?>(), AddBankContract.Presenter {
    private val model: AddBankContract.Model
    override fun getData(body: RequestBody?) {
        if (!isViewAttached) {
            return
        }
        model.getData(body)!!.enqueue(object : Callback<BankData?> {
            override fun onResponse(call: Call<BankData?>, response: Response<BankData?>) {
                mView!!.onSuccess(response.body(), response.headers()["token"])
            }

            override fun onFailure(call: Call<BankData?>, t: Throwable) {
                mView!!.onError(t)
            }
        })
    }

    override fun getBindData(body: RequestBody?) {
        if (!isViewAttached) {
            return
        }
        mView!!.showLoading()
        model.getBindData(body)!!.compose(Flo_io_main<SimpleBean>())
                .`as`(mView!!.bindAutoDispose()!!)
                .subscribe({ bean ->
                    mView!!.hideLoading()
                    mView!!.onSuccess(bean)
                }) { throwable ->
                    mView!!.hideLoading()
                    mView!!.onError(throwable)
                }
    }

    init {
        model = AddBankModel()
    }
}